//
//  ViewController.m
//  驾考助手主界面
//
//  Created by dcr on 2017/8/26.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "MainViewController.h"
#import "SelectView.h"
#import "ImageButton.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "WebViewController.h"

@interface MainViewController ()
@property(nonatomic, strong)UIButton *carTypeBtn;
@property(nonatomic, strong)SelectView *selectView;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //设置背景图
    UIImage *image = [UIImage imageNamed:@"bg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[image imageByResizeToSize:CGSizeMake(MainScreenWidth, MainScreenHeight)]];
    
    self.title = @"考驾照";
    
    [self initWidgets];
    
    _selectView = [[SelectView alloc] initWithFrame:self.view.frame andBtn:_carTypeBtn];
    [self.view addSubview:_selectView];
    _selectView.alpha = 0;
}

- (void)initWidgets{
    WS(ws);
    UILabel *label1 = [UILabel new];
    label1.clipsToBounds = YES;
    label1.layer.cornerRadius = 10;
    label1.backgroundColor = [UIColor colorWithRed:1.00 green:0.65 blue:0.00 alpha:1.00];
    label1.font = WeekFont4;
    label1.textAlignment = NSTextAlignmentLeft;
    label1.numberOfLines = 0;
    label1.text = @"科目一：理论考试\n\n共1088题";
    label1.userInteractionEnabled = YES;
    label1.tag = 1000;
    UITapGestureRecognizer *gesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];
    [label1 addGestureRecognizer:gesture1];
    
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(ws.view.mas_centerX).with.offset(-XPadding);
        make.topMargin.mas_equalTo(100);
        make.size.mas_equalTo(CGSizeMake(120, 60));
    }];

    
    UILabel *label2 = [UILabel new];
    label2.clipsToBounds = YES;
    label2.layer.cornerRadius = 10;
    label2.backgroundColor = [UIColor colorWithRed:0.00 green:0.69 blue:1.00 alpha:1.00];
    label2.font = WeekFont4;
    label2.textAlignment = NSTextAlignmentRight;
    label2.numberOfLines = 0;
    label2.text = @"科目二：小路考\n\n共9个视频";
    label2.userInteractionEnabled = YES;
    label2.tag = 1001;
    UITapGestureRecognizer *gesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];
    [label2 addGestureRecognizer:gesture2];
    
    [self.view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(ws.view.mas_centerX).with.offset(XPadding);
        make.topMargin.mas_equalTo(100);
        make.size.mas_equalTo(CGSizeMake(120, 60));
    }];
    
    UILabel *label3 = [UILabel new];
    label3.clipsToBounds = YES;
    label3.layer.cornerRadius = 10;
    label3.backgroundColor = [UIColor colorWithRed:1.00 green:0.73 blue:0.33 alpha:1.00];
    label3.font = WeekFont4;
    label3.textAlignment = NSTextAlignmentLeft;
    label3.numberOfLines = 0;
    label3.text = @"共19个视频\n\n科目三：大路考";
    label3.userInteractionEnabled = YES;
    label3.tag = 1002;
    UITapGestureRecognizer *gesture3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];
    [label3 addGestureRecognizer:gesture3];
    
    [self.view addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(ws.view.mas_centerX).with.offset(-XPadding);
        make.topMargin.mas_equalTo(160+YPadding);
        make.size.mas_equalTo(CGSizeMake(120, 60));
    }];

    
    UILabel *label4 = [UILabel new];
    label4.clipsToBounds = YES;
    label4.layer.cornerRadius = 10;
    label4.backgroundColor = [UIColor colorWithRed:0.44 green:1.00 blue:0.50 alpha:1.00];
    label4.font = WeekFont4;
    label4.textAlignment = NSTextAlignmentRight;
    label4.numberOfLines = 0;
    label4.text = @"共900题\n\n科目四：文明考试";
    label4.userInteractionEnabled = YES;
    label4.tag = 1003;
    UITapGestureRecognizer *gesture4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];
    [label4 addGestureRecognizer:gesture4];
    
    [self.view addSubview:label4];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(ws.view.mas_centerX).with.offset(XPadding);
        make.topMargin.mas_equalTo(160+YPadding);
        make.size.mas_equalTo(CGSizeMake(120, 60));
    }];
    
    _carTypeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _carTypeBtn.clipsToBounds = YES;
    _carTypeBtn.layer.cornerRadius = 40;
    _carTypeBtn.backgroundColor = [UIColor grayColor];
    _carTypeBtn.tag = 2000;
    UIImage *image = [UIImage imageNamed:@"1"];
    [_carTypeBtn setBackgroundImage:image forState:UIControlStateNormal];
    [_carTypeBtn addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_carTypeBtn];
    [_carTypeBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerX.equalTo(ws.view.mas_centerX);
        make.topMargin.mas_equalTo(160+YPadding/2-40);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    UILabel *line = [UILabel new];
    line.font = WeekFont3;
    line.textAlignment = NSTextAlignmentCenter;
    line.backgroundColor = [UIColor clearColor];
    [line setTextColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    line.numberOfLines = 1;
    line.text = @"......................新手必看......................";
    line.userInteractionEnabled = YES;
    
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerX.equalTo(ws.view.mas_centerX);
        make.centerY.equalTo(ws.view.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(MainScreenWidth, 20));
    }];
    
    NSArray *titls = [NSArray arrayWithObjects:@"报名须知", @"新手上路", nil];
    for(int i = 0; i < titls.count; i++){
        ImageButton *btn = [ImageButton buttonWithType:UIButtonTypeSystem];
        btn.backgroundColor = [UIColor clearColor];
        btn.tag = 2001 + i;
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d", 5+i]] forState:UIControlStateNormal];
        [btn setTitle:titls[i] forState:UIControlStateNormal];
        [btn setTitleColor:GeneralColor1 forState:UIControlStateNormal];
        [btn.titleLabel setFont:WeekFont3];
        [btn verticalImageAndTitle:25.0];
        [btn addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        CGFloat x = (MainScreenWidth/4.0) + (MainScreenWidth/2.0)*i - 40;
        [btn mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(x);
            make.topMargin.mas_equalTo(MainScreenHeight/2+50);
            make.size.mas_equalTo(CGSizeMake(80, 80));
        }];
    }
}

- (void)onClickBtn:(UIButton *)btn{
    if(btn.tag == 2000){
        [UIView animateWithDuration:0.3 animations:^{
            _selectView.alpha = 1;
        }];
    }else if(btn.tag == 2001){
        WebViewController *controller = [WebViewController new];
        controller.url = @"http://m.jxedt.com/info_1/";
        controller.title = @"报名须知";
        [self.navigationController pushViewController:controller animated:YES];
    }else if(btn.tag == 2002){
        WebViewController *controller = [WebViewController new];
        controller.url = @"http://www.jxedt.com";
        controller.title = @"新手上路";
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (void)labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    UILabel *label = (UILabel *)recognizer.view;
    DLog(@"labelTouchUpInside text = %d", label.tag);
    if(label.tag == 1000){
        FirstViewController *controller = [FirstViewController new];
        [self.navigationController pushViewController:controller animated:YES];
    }else if(label.tag == 1001){
        SecondViewController *controller = [SecondViewController new];
        [self.navigationController pushViewController:controller animated:YES];
    }else if(label.tag == 1002){
        ThirdViewController *controller = [ThirdViewController new];
        [self.navigationController pushViewController:controller animated:YES];
    }else if(label.tag == 1003){
        FourthViewController *controller = [FourthViewController new];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
